package com.tao.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.tao.entity.DataResult;
import com.tao.entity.Order;
import com.tao.entity.OrderM;
import com.tao.entity.Product;
import com.tao.entity.ShoppingCar;
import com.tao.entity.UserAddress;
import com.tao.service.OrderService;
import com.tao.service.ProductService;
import com.tao.service.ShoppingCarService;
import com.tao.service.UserService;

/**
 * 订单的控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/servlet/OrderServlet")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductService productService;
	@Autowired
	private UserService userService;
	@Autowired
	private ShoppingCarService carService;
	
	
	/**
	 * @param cid 购物车id,通过购物车购买产生的订单
	 * @param pid 直接购买生成的订单 商品id
	 * @param uid 用户id 
	 * @param num 商品数量
	 */
	@RequestMapping(value={"/addOrder"},
			params={"cid","pid","uid","num"},
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8"
			)
	@ResponseBody
	public int addOrder(
			@RequestParam(value="cid") int cid,
			@RequestParam(value="pid") int pid,
			@RequestParam(value="uid") int uid,
			@RequestParam(value="num") int num,
			HttpServletResponse response){
		  //设置订单时间
		  Date now=new Date();
	      SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
		  String date=sdf.format(now);
		  System.out.println("***"+cid);
		  System.out.println("***"+pid);
		  System.out.println("***"+uid);
		  System.out.println("***"+num);
		  if(cid!=0){
			  List<ShoppingCar> goods=carService.findCarById(cid);
			  System.out.print("llll"+goods.size());
			  ShoppingCar car=goods.get(0);
			  pid=car.getGoodid();
			  uid=car.getUserid();
			  num=car.getNum();
		  }
		  System.out.println("***"+cid);
		  System.out.println("***"+pid);
		  System.out.println("***"+uid);
		  System.out.println("***"+num);
		  
		  Order order=new Order();
		  List<Product> products=productService.findProductById(pid);
		    if (products.size()> 0) {
		    	Product p=products.get(0);
		    	 double price=p.getPrice();
			     double total=price*num;
			     order.setTotal(total);
		    }
		     
		    
		  //从数据库中获取地址，联系电话和收件人   
		   List<UserAddress> users=userService.findAddressById(uid);
		   if (users.size() > 0) {
			   UserAddress add=users.get(0);
			    String sjrname=add.getName();
			    String phone=add.getPhone();
			    String address=add.getAddress();
		   }
		  
		    
		  order.setProuductid(pid);
		  order.setUser(uid);
		  order.setDate(date);
//		  order.setAddress(address);
//		  order.setPhone(phone);
//		  order.setSjrname(sjrname);
		  order.setNum(num);
		  order.setAddress("山东烟台");
		  order.setPhone("18865515995");
		  order.setSjrname("收件人");
		  int s=orderService.addOrder(order);
		 return s;
	}
	
	
	/**
	 * @param cid 购物车id,通过购物车购买产生的订单
	 * @param pid 直接购买生成的订单 商品id
	 * @param uid 用户id 
	 * @param num 商品数量
	 */
	@RequestMapping(value={"/addMultiOrder"},
			params={"orderAddMulti","uid"},
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8"
			)
	@ResponseBody
	public void addMultiOrder(
			@RequestParam(value="orderAddMulti") String orders,
			@RequestParam(value="uid") int uid,
			HttpServletResponse response){
		 Gson gson = new Gson();
		 OrderM orderM = gson.fromJson(orders, OrderM.class);
         List<OrderM.ListBean> items = orderM.getList();
         for (int i = 0; i < items.size(); i++) {
        	 int s = addOrder(0, items.get(i).getPid(), uid,items.get(i).getNum(),response);
		}
		 
//		 return 0;
	}
	
	
	@RequestMapping(value={"/findAllOrderByUserid"},
			params={"id"},
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8"
			)
	@ResponseBody
	public  DataResult<Order> findAllOrderByUserid(
			@RequestParam(value="id") int id,
			HttpServletResponse response){
		 List<Order> list=orderService.findAllOrderByUserid(id);
		 DataResult<Order> dt=new DataResult<Order>(200, list, list.size());
		 return dt;
	}
	
	//删除订单
	@RequestMapping(value = { "/deleteOrder" }, 
			     params = { "orderids" }, 
			     method = RequestMethod.POST)
	@ResponseBody
	public int deleteCar(@RequestParam(value = "orderids") String orderids) {
		System.out.println(orderids);
		int s = orderService.deleteOrder(orderids);
		return s;
}
}
